import time

from selenium import webdriver
from selenium.webdriver.support.select import Select
from selenium.webdriver.common.by import By

driver = webdriver.Chrome("C:\Program Files\driver\chromedriver.exe")
driver.maximize_window()

driver.get("file:///C:/TT/%E4%B9%A0%E7%9F%A5/%E8%87%AA%E5%8A%A8%E5%8C%96%E4%BB%A3%E7%A0%81-8%E6%9C%9F/myStudyDemo/seleniumDemo1/selectDemo.html")
time.sleep(3)
# 定位下拉框元素
element_select = driver.find_element(By.CSS_SELECTOR, "select")

# 实例化一个下拉框对象
select_obj = Select(element_select)

# # 通过索引选择，index 索引从 0 开始
# select_obj.select_by_index(2)
# time.sleep(3)
# select_obj.select_by_index(4)
# time.sleep(3)
# select_obj.select_by_index(1)
# time.sleep(3)

# # 通过值选择(option标签的一个属性值)
# select_obj.select_by_value('chengdu')
# time.sleep(3)
# select_obj.select_by_value('shanghai')
# time.sleep(3)
# select_obj.select_by_value('beijing')
# time.sleep(3)

# 通过文本选择(下拉框的值)
select_obj.select_by_visible_text("北京")
time.sleep(3)
select_obj.select_by_visible_text("山东")
time.sleep(3)
select_obj.select_by_visible_text("深圳")
time.sleep(3)

driver.quit()